<template>
    <!-- 对话框表单 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :destroy-on-close="true"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="config.layout.shrink ? '' : 'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    ref="formRef"
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    :label-position="config.layout.shrink ? 'top' : 'right'"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                    v-if="!baTable.form.loading"
                >
                    <el-form-item prop="nickname" :label="t('shop.user.user.nickname')">
                        <el-input
                            v-model="baTable.form.items!.nickname"
                            type="string"
                            :placeholder="t('Please input field', { field: t('shop.user.user.nickname') })"
                        ></el-input>
                    </el-form-item>
                    <el-form-item prop="mobile" :label="t('shop.user.user.mobile')">
                        <el-input
                            v-model="baTable.form.items!.mobile"
                            type="string"
                            :placeholder="t('Please input field', { field: t('shop.user.user.mobile') })"
                        ></el-input>
                    </el-form-item>
                    <FormItem
                        v-model="baTable.form.items!.avatar"
                        :input-attr="{
                            limit: 1,
                            hideImagePlusOnOverLimit: true,
                        }"
                        :label="t('shop.user.user.avatar')"
                        type="image"
                    />
                    <FormItem
                        v-model="baTable.form.items!.qr"
                        :input-attr="{
                            limit: 1,
                            hideImagePlusOnOverLimit: true,
                        }"
                        :label="t('视频号二维码')"
                        type="image"
                    />
                    <el-form-item prop="nickname" :label="t('邀请码')">
                        <el-input
                            v-model="baTable.form.items!.code"
                            type="string"
                            :placeholder="t('Please input field', { field: t('邀请码') })"
                        ></el-input>
                    </el-form-item>
<!--                    <FormItem-->
<!--                        :label="t('shop.user.user.gender')"-->
<!--                        v-model="baTable.form.items!.gender"-->
<!--                        type="radio"-->
<!--                        :input-attr="{-->
<!--                            border: true,-->
<!--                            content: { 0: t('shop.user.user.gender 0'), 1: t('shop.user.user.gender 1'), 2: t('shop.user.user.gender 2') },-->
<!--                        }"-->
<!--                    />-->
<!--                    <FormItem-->
<!--                        type="remoteSelect"-->
<!--                        prop="grade_id"-->
<!--                        :label="t('shop.user.user.grade')"-->
<!--                        v-model="baTable.form.items!.grade_id"-->
<!--                        :placeholder="t('Click select')"-->
<!--                        :input-attr="{-->
<!--                            pk: 'ba_shop_user_grade.id',-->
<!--                            field: 'name',-->
<!--                            'remote-url': '/admin/shop.user.grade/index',-->
<!--                        }"-->
<!--                    />-->
                    <el-form-item :label="t('shop.user.user.birthday')">
                        <el-date-picker
                            class="w100"
                            value-format="YYYY-MM-DD"
                            v-model="baTable.form.items!.birthday"
                            type="date"
                            :placeholder="t('Please select field', { field: t('shop.user.user.birthday') })"
                        />
                    </el-form-item>
                    <FormItem
                        :label="t('State')"
                        v-model="baTable.form.items!.status"
                        type="radio"
                        :input-attr="{
                            border: true,
                            content: { 0: t('Disable'), 1: t('Enable') },
                        }"
                    />
                    <FormItem
                        type="remoteSelect"
                        prop="pid"
                        :label="t('shop.user.user.pid')"
                        v-model="baTable.form.items!.pid"
                        :placeholder="t('Click select')"
                        :input-attr="{
                            pk: 'user.id',
                            field: 'nickname',
                            'remote-url': '/admin/shop.user.User/index',
                            onChange: getAdd,
                            tooltipParams: {
                                '用户名': 'username'
                            }
                        }"
                    />
                    <FormItem
                        type="remoteSelect"
                        prop="level_id"
                        :label="t('推荐等级')"
                        v-model="baTable.form.items!.level_id"
                        :placeholder="t('Click select')"
                        :input-attr="{
                            pk: 'ba_recommend_level.id',
                            field: 'name',
                            'remote-url': '/admin/recommend.level/index',
                        }"
                    />
                    <FormItem
                        type="remoteSelect"
                        prop="partner_id"
                        :label="t('合伙人')"
                        v-model="baTable.form.items!.partner_id"
                        :placeholder="t('Click select')"
                        :input-attr="{
                            pk: 'ba_recommend_partner.id',
                            field: 'name',
                            'remote-url': '/admin/recommend.partner/index',
                        }"
                    />

                    <el-form-item prop="pnum" :label="t('可派单数量')">
                        <el-input
                            v-model="baTable.form.items!.pnum"
                            type="number"
                            :input-attr="{ step: 1 }"
                            :placeholder="t('Please input field', { field: t('可派单数量') })"
                        ></el-input>
                    </el-form-item>
                    <FormItem
                        :label="t('派单状态')"
                        v-model="baTable.form.items!.pstatus"
                        type="radio"
                        :input-attr="{
                            border: true,
                            content: { 0: t('Disable'), 1: t('Enable') },
                        }"
                    />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm('')">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive, inject } from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import type { FormInstance, FormItemRule } from 'element-plus'
import FormItem from '/@/components/formItem/index.vue'
import { buildValidatorData } from '/@/utils/validate'
import { useConfig } from '/@/stores/config'

const config = useConfig()
const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    nickname: [buildValidatorData({ name: 'required', title: t('shop.user.user.nickname') })],
    mobile: [buildValidatorData({ name: 'mobile' })],
})
</script>

<style scoped lang="scss">
.avatar-uploader {
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    border-radius: var(--el-border-radius-small);
    box-shadow: var(--el-box-shadow-light);
    border: 1px dashed var(--el-border-color);
    cursor: pointer;
    overflow: hidden;
    width: 110px;
    height: 110px;
}
.avatar-uploader:hover {
    border-color: var(--el-color-primary);
}
.avatar {
    width: 110px;
    height: 110px;
    display: block;
}
.image-slot {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
}
</style>
